﻿using System;
using System.Text;
using System.Web.UI.WebControls;

namespace BLL
{
    public static class MessageBox
    {
        /// <summary>
        /// 显示消息对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">信息</param>
        public static void Show(this System.Web.UI.Page page, string msg)
        {
            var text = new StringBuilder();
            text.AppendLine("if(typeof top.MessageBox != \"undefined\"){");
            text.AppendLine("   top.MessageBox.Alert(\"" + msg + "\");");
            text.AppendLine("}");
            text.AppendLine("else{");
            text.AppendLine("alert(\"" + msg + "\");");
            text.AppendLine("}");
            ResponseScript(page, text.ToString());
        }

        /// <summary>
        /// 控件点击 消息确认框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">信息</param>
        public static void ShowConfirm(this WebControl Control, string msg)
        {
            //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
            Control.Attributes.Add("onclick", "return confirm('" + msg + "?" + "');");
        }

        /// <summary>
        /// 显示消息对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.AppendLine("if(typeof top.MessageBox != \"undefined\"){");
            Builder.AppendLine("   var option={");
            Builder.AppendLine("                Msg:\"" + msg + "\",");
            Builder.AppendLine("                YesCallBack:function(index){");
            Builder.AppendLine("                                location.href=\"" + url + "\";");
            Builder.AppendLine("                                top.$(\"#layui-layer\" + index + \" .layui-layer-close\").click();");
            Builder.AppendLine("                }");
            Builder.AppendLine("   };");
            Builder.AppendLine("   top.MessageBox.Alert(option);");
            Builder.AppendLine("}");
            Builder.AppendLine("else{");
            Builder.AppendLine("    alert(\"" + msg + "\");");
            Builder.AppendLine("    location.href=\"" + url + "\";");
            Builder.AppendLine("}");
            ResponseScript(page, Builder.ToString());
        }

        /// <summary>
        /// 显示消息对话框，并关闭当前页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        public static void ShowAndRedirectClose(System.Web.UI.Page page, string msg)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.AppendLine("if(typeof top.MessageBox != \"undefined\"){");
            Builder.AppendLine("   var option={");
            Builder.AppendLine("                Msg:\"" + msg + "\",");
            Builder.AppendLine("                YesCallBack:function(index){");
            Builder.AppendLine("                                top.$(\"#layui-layer\" + index + \" .layui-layer-close\").click();");
            Builder.AppendLine("                }");
            Builder.AppendLine("   };");
            Builder.AppendLine("   top.MessageBox.Alert(option);");
            Builder.AppendLine("}");
            Builder.AppendLine("else{");
            Builder.AppendLine("    alert(\"" + msg + "\");");
            Builder.AppendLine("    window.close();");
            Builder.AppendLine("}");
            ResponseScript(page, Builder.ToString());
        }

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(this System.Web.UI.Page page, string script)
        {
            if (page.ClientScript.IsStartupScriptRegistered(page.GetType(), "defer"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), "<script language='javascript'>" + script + "</script>");
            }
            else
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), "<script language='javascript' defer>" + script + "</script>");
            }
        }
    }
}